/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.security.data;

import com.telsoft.security.entity.SEC_ETT_GroupRight;
import com.telsoft.security.entity.SEC_ETT_Right;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nguyenthang
 */
public class SEC_DATA_GROUP_RIGHT {

    private Connection connect = null;
    private SEC_ETT_Right right;
    private List<SEC_ETT_Right> AllRight;

    public List<SEC_ETT_Right> getAllRightByGroupID(int GROUP_ID) {
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        AllRight = new ArrayList<SEC_ETT_Right>();
        PreparedStatement stmt;
        ResultSet rs;
        try {
            connect = connectDB.dbConnect();
            String query = "SELECT * FROM SEC_RIGHT_GROUP WHERE GROUP_ID = ?";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, GROUP_ID);
            rs = stmt.executeQuery();
            while (rs.next()) {
                right = new SEC_ETT_Right();
                SEC_DATA_Right DR = new SEC_DATA_Right();
                right = DR.getAllByRightID(rs.getInt("RIGHT_ID"));
                AllRight.add(right);
            }
            connectDB.dbClose(connect, stmt, rs);
        } catch (Exception ex) {
            ex.toString();
        }
        return AllRight;
    }
    public void deleteGRByID(int GroupID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "DELETE FROM SEC_RIGHT_GROUP WHERE GROUP_ID = ?";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, GroupID);
            connectDB.dbClose(connect);
            connectDB.dbClose(stmt);
        } catch (Exception ex){
            ex.toString();
        }
    }
    public void addGroupRight(int groupID, int rightID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "INSERT INTO SEC_RIGHT_GROUP (RIGHT_ID, GROUP_ID) VALUE (?,?)";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, rightID);
            stmt.setInt(2, groupID);
            stmt.execute();
            connectDB.dbClose(connect);
            connectDB.dbClose(stmt);
        } catch (Exception ex){
            ex.toString();
        }
    }
}
